
// 封装倒计时函数逻辑

import { ref,computed, onUnmounted } from "vue"
import dayjs from "dayjs"

export const useCutdown = ()=>{
  // 函数没有参数，但是有两个返回值
  const time = ref(0)
  const formatTime = computed(()=> dayjs.unix(time.value).format('mm分ss秒'))
  let timer = null

  const start =(cutdownTime)=>{
    time.value = cutdownTime
    // 倒计时逻辑
    timer = setInterval(() => {
      time.value--
    }, 1000);
  }
  // 优化 在组件卸载时候清除定时器
  onUnmounted(()=>{
    timer&&clearInterval(timer)
  })
  return {
    formatTime,start
  }
}